import java.util.concurrent.ThreadLocalRandom;

/**
 * 机器,完成工序的设备
 *
 * @author sk.z
 */
public class Machine {

    private int id;

    private String name;

    private int quality;    //合格率 90% 表达为90

    private int capacity;   //产能

    private int produced = 0;   //已生产数量

    public Machine(int id, String name, int quality, int capacity) {
        this.id = id;
        this.name = name;
        this.quality = quality;
        this.capacity = capacity;
    }

    //加工
    public int process(Product product, int stageIndex) {
        if (capacity <= produced) {
            return 0;
        }
        produced++;

        //是否合格
        if (quality > ThreadLocalRandom.current().nextInt(100)) {
            product.setResult(stageIndex, 1);
        } else {
            product.setResult(stageIndex, 2);
        }
        return 1;
    }

    @Override
    public String toString() {
        return "机器{" + name +
                ", 合格率=" + quality +
                ", 产能=" + capacity +
                ", 实产=" + produced + ", 富余=" + (this.capacity-this.produced)+
                '}';
    }
}
